package logic

import (
	"context"
	"github.com/google/uuid"
	"github.com/zeromicro/go-zero/core/logx"
	"srv/internal/svc"
	"srv/model/mysql"
	"srv/pkg"
	"srv/shenzhou"
	"strconv"
)

type CreateOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateOrderLogic) CreateOrder(in *shenzhou.CreateOrderRequest) (*shenzhou.CreateOrderResponse, error) {
	orderSn := uuid.NewString()
	amount := 8765
	o := mysql.Order{
		GoodsId: int(in.GoodsId),
		OrderSn: orderSn,
		Amount:  float64(amount),
	}
	err := o.AddOrder()
	if err != nil {
		logx.Error("订单添加失败")
		return nil, err
	}

	url := pkg.AliPay(orderSn, strconv.Itoa(amount))
	return &shenzhou.CreateOrderResponse{
		OrderId: int64(o.ID),
		PayUrl:  url,
	}, nil
}
